package com.ccpdown.view;

import java.util.List;

import com.ccpdown.view.R;
import com.ccpdown.adapter.IconfiedTextListAdapter;
import com.ccpdown.util.StringUtil;
import com.ccpdown.util.chkPackage.ChkApplicationUitl;
import com.ccpdown.util.file.Updater;
import com.ccpdown.util.socket.NetWorkUtil;
import com.ccpdown.util.xml.CtrXmlUtil;
import com.ccpdown.vo.ConfiedText;

import android.R.color;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainList extends ListActivity {

	private Button btnTurnApplies;
	private Button btnTurnGames;

	private AlertDialog.Builder builder;

	private List<ConfiedText> item;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findview();
		if (NetWorkUtil.chkNetWork(this)) {
			getListener();
			showList(StringUtil.ROOTURL+StringUtil.FIRURL+1);
		} else {
			builder.setTitle("请查看网络连接");
			builder.setMessage("本软件需要进行网络连接才能运行，请查看网络连接");
			builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			builder.show();
		}
	}

	// 选单设置；
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "关于");
		menu.add(0,Menu.FIRST+3,0,"软件升级");
		menu.add(0, Menu.FIRST + 2, 0, "帮助");
		menu.add(0, Menu.FIRST + 1, 0, "结束");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST:
			openOptionsDialog("关于CC玩家market", "欢迎使用CC玩家market，目前版本为1.0_alpha");
			break;
		case Menu.FIRST + 2:
			openOptionsDialog("关于CC玩家market", "欢迎使用CC玩家market，目前版本为1.0_alpha");
			break;
		case Menu.FIRST + 1:
			finish();
			break;
		case Menu.FIRST+3:
			Updater.checkUpdate(this, StringUtil.ROOTURL+StringUtil.SOFTURL+StringUtil.SOFTID);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void findview() {
		btnTurnApplies = (Button) findViewById(R.id.btnTurnApplies);
		btnTurnGames = (Button) findViewById(R.id.btnTurnGames);
		builder = new Builder(MainList.this);
	}

	private void getListener() {
		btnTurnApplies.setOnClickListener(btnTurnAppliesClick);
		btnTurnGames.setOnClickListener(btnTurnGamesClick);
	}

	private void showList(String path) {
		// InputStream is =
		// SaxReadXml.class.getClassLoader().getResourceAsStream("type.xml");
		item = CtrXmlUtil.getTypeFromPath(path, 1);

		try {
			if (item != null) {
				IconfiedTextListAdapter itla = new IconfiedTextListAdapter(
						this, item);
				setListAdapter(itla);
			}
		} catch (NullPointerException ex) {
			ex.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void openOptionsDialog(String title, String message) {
		builder.setTitle(title);
		builder.setMessage(message);
		builder.show();
	}

	// 各种监听
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		String typeId = ((ConfiedText) this.item.get((int) id)).typeId;
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("typeId", typeId);
		intent.putExtras(bundle);
		intent.setClass(MainList.this, SoftList.class);
		startActivity(intent);
	}

	private OnClickListener btnTurnAppliesClick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showList(StringUtil.ROOTURL+StringUtil.FIRURL+1);
			btnTurnApplies.setBackgroundDrawable(getResources().getDrawable(R.drawable.title3));
			btnTurnApplies.setTextColor(Color.DKGRAY);
			btnTurnGames.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg1));
			btnTurnGames.setTextColor(Color.WHITE);
		}
	};

	private OnClickListener btnTurnGamesClick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showList(StringUtil.ROOTURL+StringUtil.FIRURL+2);
			btnTurnApplies.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg1));
			btnTurnApplies.setTextColor(Color.WHITE);
			btnTurnGames.setBackgroundDrawable(getResources().getDrawable(R.drawable.title3));
			btnTurnGames.setTextColor(Color.DKGRAY);
		}

	};

}